import request from "../../utils/request";

export const LOGIN_TYPE = "login";
export const LOGOUT_TYPE = "logout";

// 同步写法
export const login = function (payload) {
  return {
    type: LOGIN_TYPE,
    payload,
  };
};
export const logout = function () {
  return {
    type: LOGOUT_TYPE,
  };
};
export const updateUser = function () {
  return {
    type: "update",
  };
};

// 异步写法：必须使用redux-thunk中间件
// 让dispatch支持函数
export function loginAsync(params) {
  return async function (dispatch) {
    const { data } = await request.get("/user/login", {
      params,
    });

    dispatch(login(data.data));
    return data;
  };
}
// 调用方式：
// dispatch(loginAsync({username:'laoxie',password:1234}))

export default {
  login,
  logout,
  loginAsync,
};
